package view.GUI.sidepanels;

import controller.IListener;
import controller.commands.BasicCommand;
import controller.commands.ClearSelectionCommand;
import controller.commands.SetSelectedCommand;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import model.graphic.GraphicModel;
import model.graphic.objects.Graphical;
import view.GUI.graphic.GraphicView;

/**
 *
 * @author Ben
 */
public class InventoryPanel extends JPanel implements TreeSelectionListener,IListener {
    private JTree tree;
    private GraphicView gview;
    private InventoryPanelModel model;

    public InventoryPanel(GraphicModel gmodel,GraphicView gv) {
        this.gview = gv;
        model = new InventoryPanelModel(gmodel);
        tree = new JTree(model);
        tree.setCellRenderer(new InventoryPanelRenderer());
        tree.addTreeSelectionListener(this);
        gview.addObserver(this);
        this.add(tree);
        this.setBackground(Color.WHITE);
    }

    public void valueChanged(TreeSelectionEvent arg0) {
        Object node = tree.getLastSelectedPathComponent();
        if (node != null && Graphical.class.isAssignableFrom(node.getClass())){
            gview.notifyObservers(new ClearSelectionCommand());
            gview.notifyObservers(new SetSelectedCommand((Graphical)node, true));
            gview.notifyObservers(new CustomArgument("tabRight", 0));
            gview.repaint();
        }
    }

    public void update(BasicCommand com) {
         model.reload();  //model.nodeChanged(treeNode);
    }

}
